layui.use(['table', 'laydate', 'form'], function () {
    var table = layui.table;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
        elem: '#rxDate', //指定元素
        type: 'datetime'
    });

    //获取select 可选项
    var selectMarket = []
    $.get({
        url: '/scTuitionApi/getMarketingList',
        data: {},
        async:false,
        success: res => {
            selectMarket = res.data
            $.each(res.data, function (i) {
                $('select[name=studentMarket]').append(`<option   value="` + res.data[i].marketingId + `">`
                    + res.data[i].marketingName + `</option>`)
            })
            form.render('select')
        }
    })


    //获取select 可选项
    var select = []
    $.get({
        url: '/scTuitionApi/getAllStandardUseing',
        data: {},
        async:false,
        success: res => {
            select = res.data
            $.each(res.data, function (i) {
                $('select[name=studentSchoolTerm]').append(`<option   value="` + res.data[i].id + `">`
                    + res.data[i].classesPeriods + `期  (` + res.data[i].startTime +
                    `标准)</option>`)
            })
            form.render('select')
        }
    })


    form.on('select(close-semester)', function (d) {
        $.get({
            url: '/scTuitionApi/getAllThisYearStandard',
            data: {classesPeriods: d.value},
            success: res => {
                $('input[name=documentMoney]').val(res.data.classesMoney)
            }
        })
    })


    $("#xz").click(function () {
        var index = layer.open({
            type: 2,
            title: '选择入学班级',
            content: 'classifyClassTree.html',
            area: ['80%', '80%']
        })
    })

    //保存订单和学生数据
    form.on('submit(*)', function (d) {

        if (isName(d.field.studentName)){
            layer.msg("真实姓名填写有误",{icon:5,time:1000})
        }else if(isCardNo(d.field.studentIdNumber)){
            layer.msg("身份证输入不合法",{icon:5,time:1000})
        }else {
            var id = d.field.studentSchoolTerm
            $.each(select,function (i){
                if (select[i].id == id){
                    d.field.studentSchoolTerm = select[i].classesPeriods
                    return
                }
            })
            d.field.standardId = id
            // 添加学生信息
            $.get({
                url: '/stufee/addStudent',
                data: d.field,
                success: function (res) {
                    if (res.state == "200"){
                        layer.msg('添加成功,前往学生交费表格进行查看',{icon:6,time:2000},function (){
                            location.reload();
                        })
                    }else {
                        layer.msg('添加失败',{icon:5,time:2000},function (){
                            location.reload();
                        })
                    }
                },
                error: function (e) {
                    layer.msg('请求异常，请填写正确的学生信息',{icon:5,time:2000},function (){
                        location.reload();
                    })
                }
            })
        }

    })

    //重置input输入框的内容
    form.on('submit(cz)', function (d) {
        layer.msg('重置成功',{icon:6,time:1000},function (){
            location.reload();
        })
    })
})

//获取当前时间
var myDate = new Date();
//获取当前时间戳
var timestamp = Date.parse(myDate);
//当做订单编号
$("#dj").text(timestamp)
$("[name=documentId]").val(timestamp)
//获取完整的年份(4位)
var year = myDate.getFullYear();
//获取当前月份(0-11,0代表1月)
var month = myDate.getMonth();
month = month + 1
//获取当前日(1-31)
var day = myDate.getDate();
// 添加分隔符“-”
var seperator = "-";

// 对月份进行处理，1-9月在前面添加一个“0”
if (month >= 1 && month <= 9) {
    month = "0" + month;
}

// 对月份进行处理，1-9号在前面添加一个“0”
if (day >= 0 && day <= 9) {
    day = "0" + day;
}

// 最后拼接字符串，得到一个格式为(yyyy-MM-dd)的日期
// var nowDate = year + seperator + month + seperator + day
//设置给入学日期
var nowDate = CurentTime();
$("#rxDate").val(nowDate)
//学号：
year = year + "";
var a = year.slice(2, 4);
var xh = a + month + day;

//获取4位随机数
do
    out = Math.floor(Math.random() * 10000);
while (out < 1000)
$("[name=studentId]").val(xh + out)


//接收子页面传来的数据
function subData(a, b)
{
    $("[name=studentClasses]").val(a)
    $("[name=classesName]").val(b)
}


//获取当前时间
function CurentTime()
{
    var now = new Date();
    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日

    var hh = now.getHours();            //时
    var mm = now.getMinutes();          //分

    var clock = year + "-";

    if(month < 10)
        clock += "0";

    clock += month + "-";

    if(day < 10)
        clock += "0";

    clock += day + " ";

    if(hh < 10)
        clock += "0";

    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm;
    clock += ":00";
    return(clock);
}

//身份证校验
function isCardNo(card)
{
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if(!reg.test(card))
    {
        return  true;
    }
}


//姓名校验
function isName(name)
{
    var regName =/^[\u4e00-\u9fa5]{2,4}$/;
    if(!regName.test(name)){
        return true;
    }

}


